using System;
using OhioState.AssetManaged;
using OhioState.Graphics;

namespace OhioState.Graphics.OpenGL
{
    public class OpenGLObject : MarshalByRefObject, IDisposable
    {
        #region Constructors
        protected OpenGLObject(IAssetManaged proxyObject) : this(proxyObject,null)
        {
        }

        protected OpenGLObject(IAssetManaged proxyObject, IAssetManagedHandler e)
        {
            this.proxyObject = proxyObject;
            if (e != null)
                proxyObject.AddChangedEventHandler(e);
            else
                proxyObject.AddChangedEventHandler(invalidateState);
        }
        #endregion

        #region State Invadiation
        public bool Invalid
        {
            get { return _invalid; }
            protected set { _invalid = value; }
        }

        /// have to make this method public for remoting to work correctly:
        /// methods that are called remotely should be public
        public virtual void Update(IRenderPanel panel)
        {
        }

        public void invalidateState(IAssetManaged obj)
        {
            this.Invalid = true;
        }
        #endregion

        public virtual void Dispose()
        {
            proxyObject.RemoveChangedEventHandler(invalidateState);
            proxyObject.RemoveChangingEventHandler(invalidateState);
        }

        #region Member variables
        protected IAssetManaged proxyObject;
        private volatile bool _invalid = true;
        #endregion
    }
}
